package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserMineWarPatronsData;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMineWarPatronsDaoImpl extends TempDataObjectDao<UserMineWarPatronsData> {

    public Map<Integer, UserMineWarPatronsData> getUserMineWarPatronsDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_mine_patrons where userId=?";
        Map<Integer, UserMineWarPatronsData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMineWarPatronsData data = parse(rs);
                dataMap.put(data.getPatronsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserMineWarPatronsData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_mine_patrons(userId,patronsId,battleTimes,buyBattleTimes,dispatchMine,modifyTime,type,pitId) VALUES(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++, data.getBattleTimes());
            call.setInt(i++, data.getBuyBattleTimes());
            call.setInt(i++, data.getDispatchMine());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getPitId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserMineWarPatronsData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_mine_patrons SET battleTimes=?,buyBattleTimes=?,dispatchMine=?,modifyTime=?,type=?,pitId=? WHERE userId=? and patronsId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBattleTimes());
            call.setInt(i++, data.getBuyBattleTimes());
            call.setInt(i++, data.getDispatchMine());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getPitId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public boolean updateUserMineWarPatronsData(long userId, List<Integer> patronsIdList, int dispatchMine, int type, int pitId) {
        boolean result = false;
        String sql = "UPDATE t_u_mine_patrons SET dispatchMine=?,type=?,pitId=? WHERE userId=? and patronsId=?";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (int patronsId : patronsIdList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, dispatchMine));
            para.put(i++, new DbParameter(Types.INTEGER, type));
            para.put(i++, new DbParameter(Types.INTEGER, pitId));
            para.put(i++, new DbParameter(Types.BIGINT, userId));
            para.put(i++, new DbParameter(Types.INTEGER, patronsId));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    @Override
    protected UserMineWarPatronsData parseData(ResultSet rs) throws SQLException {
        UserMineWarPatronsData data = new UserMineWarPatronsData();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        data.setBuyBattleTimes(rs.getInt("buyBattleTimes"));
        data.setDispatchMine(rs.getInt("dispatchMine"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setType(rs.getInt("type"));
        data.setPitId(rs.getInt("pitId"));
        return data;
    }
}
